<?php
namespace app\admin\controller;

use app\common\component\user\UserModel;
use app\common\model\UserActivation as UserActivationModel;
use app\common\model\UserActivationLog as UserActivationLogModel;

use app\common\component\user\UserService;
use app\common\component\user_activation\UserActivationService;
use app\common\component\user_activation_log\UserActivationLogService;

class UserActivation extends ControllerBase {
	public function _initialize() {
		$this -> checkLogin();

		$this -> checkAuthorization();
	}

	public function check() {
		$this -> view -> assign('userActivationLogs', UserActivationLogModel::order('is_state ASC, user_activation_log_id DESC') -> paginate(10));

		return $this -> view -> fetch();
	}

	public function handleCheck() {
		$getUserActivationLog = UserActivationLogModel::get(['user_activation_log_id' => $this -> request -> get('user_activation_log_id', 0, 'int')]);

		$findUser = UserModel::where('username', $getUserActivationLog['to_user_name']) -> find();
		if(!$findUser) {
			echo '<meta charset="utf-8"><script>alert("用户不存在");window.location.href="/shuashuale/user/activation/check";</script>';exit;
		}

		$resultGetUserActivation = (new UserActivationService) -> getUserActivationId($findUser['user_id']);

		$getUserActivationLog -> to_user_name_new_price = $resultGetUserActivation['data']['user_activation'] -> price + $getUserActivationLog['price'];
		$getUserActivationLog -> to_user_name_old_price = $resultGetUserActivation['data']['user_activation'] -> price;
		$getUserActivationLog -> is_state = 1;

		\think\Db::startTrans();
		try {
			if($getUserActivationLog -> save()) {
				if(!UserActivationModel::where('user_id', $findUser['user_id']) -> setInc('price', $getUserActivationLog['price'])) {
					throw new \Exception('审核失败');
				}
			} else {
				throw new \Exception('审核失败');
			}

			\think\Db::commit();
		} catch(\Exception $e) {
			\think\Db::rollback();
			echo '<meta charset="utf-8"><script>alert("' . $e -> getMessage() . '");window.location.href="/shuashuale/user/activation/check";</script>';exit;
		}
		echo '<meta charset="utf-8"><script>alert("审核成功");window.location.href="/shuashuale/user/activation/check";</script>';exit;
	}

	public function topUp() {
		return $this -> view -> fetch();
	}

	public function handleTopUp() {
		$post = $this -> request -> post();

		$findUser = UserModel::where('username', $post['to_user_name']) -> find();
		if(!$findUser) {
			echo '<meta charset="utf-8"><script>alert("用户不存在");window.location.href="/shuashuale/user/activation/topUp";</script>';exit;
		}
		
		\think\Db::startTrans();
		try {
			$resultUserActivationLogServiceLog = (new UserActivationLogService()) -> log(1, 1, $post['price'], $post['to_user_name'], $post['to_user_name'], '后台激活码充值', 1);
			if($resultUserActivationLogServiceLog['code'] != 0) {
				throw new \Exception($resultUserActivationLogServiceLog['message']);
			}

			if(!UserActivationModel::where('user_id', $findUser['user_id']) -> setInc('price', $post['price'])) {
				throw new \Exception('激活码充值失败');
			}

			\think\Db::commit();
		} catch(\Exception $e) {
			\think\Db::rollback();
			echo '<meta charset="utf-8"><script>alert("' . $e -> getMessage() . '");window.location.href="/shuashuale/user/activation/topUp";</script>';exit;
		}

		echo '<meta charset="utf-8"><script>alert("激活码充值成功");window.location.href="/shuashuale/user/activation/topUp";</script>';exit;
	}
}